//
// submit Form function (POST with ajax)
//
  var formID = null;
  var formBtnText = null;
  var formOKfunction = null;
  
  
 function submitForm(ID, submitOK)
  {	
  	formID = ID;
	formOKfunction = submitOK;
	
	// Disable form
	//disableForm(formID, true);

 	$(formID).request({
		onLoading: function() {
 					if($(formID + 'Loading'))
 						$(formID + 'Loading').addClassName('isLoading');
					disableForm(formID, true);
				},
 		onSuccess: function(transport){
		     
			var validationResult = transport.responseJSON;
			
			// check if json is all NULL
			if(!validationResult)
			{
				alert("critical error");
				return false;
			}
			// clear all alerts on form
			clearFormAlerts();
			
			// show form validation
			if(validationResult.errorlist)
				showFormValidation(validationResult);
			else
				formOKfunction();
			// enable form
			disableForm(formID, false);
 		}
 	});	

    return false;
  }
  
//
//	Aplicates validation on form
//
function showFormValidation(validationResult)
{
	
	if(validationResult.errorlist != 1) {
		validationResult.errorlist.each(function(s){
			if($(s['id']))
				$(s['id']).addClassName('alertERROR');
		});
	}

	// show error msg
	$(validationResult.key +'Error').hide();
	if(validationResult.errorlist != 1) {
		
		var errorText = "";
		validationResult.errorlist.each(function(s){
			errorText += '* ' + s['msg']+'<br>';
		});
		if($(validationResult.key +'ErrorContent'))
			$(validationResult.key +'ErrorContent').update(errorText);
	}
	else
		if($(validationResult.key +'Error'))
			$(validationResult.key +'Error').update(validationResult.msg)

	if($(validationResult.key +'Error'))
		$(validationResult.key +'Error').show();

	

	// close waiting dialog
	if($(formID + 'Loading'))
		$(formID + 'Loading').removeClassName('isLoading');


}
  

//
//	Default way when form is OK
//
function defaultSubmitOK( OKformID, returnMSG )
{
	var rtMSG = ""
	for(c=6;returnMSG[c];c++)
		rtMSG = rtMSG + returnMSG[c];
		
	rtMSG = "<div id='rtMSG'>" + rtMSG + "</div>";
	
	$( OKformID+'_Container').innerHTML = rtMSG;
}


//
//	implode checklist
//
function implodeChecklist(checklistDIV, inputRes)
{
	$(inputRes).value = '';
	first=true;
	$$('#'+checklistDIV+' input').each( function(e){
		if (e.checked) {
			if(first==false) $(inputRes).value += ',';
			$(inputRes).value += e.name;
			first = false;
		}

	}.bind(this));
}


//
//	Disable Form
//
function disableForm(formID, isdisable)
{
	$$('#'+formID+'Container input').each(function(s, index) {
  		s.disabled=isdisable;
	});	
}

//
//	Clear data Alerts
//
function clearFormAlerts(){
	$$('#'+formID+'Container .alertERROR').each(function(s, index) {
  		s.removeClassName('alertERROR');
	});	
}
